package com.bidianying.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.bidianying.util.JsonUtil;

@Repository
public class CommonDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public List<Map<String,String>> getAllTaobaoCinemaInfo(){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		String sql = "select * from tTaobao_cinema";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		while(rs.next()){
			Map<String,String> map = new HashMap<String,String>();
			map.put("cinemaName", rs.getString("cinemaName"));
			map.put("cinemaId", rs.getString("cinemaId"));
			map.put("cityPinyin", rs.getString("cityPinyin"));
			resultList.add(map);
		}
		return resultList;
	}
	
	public List<Map<String,String>> getAllMaizuoCinemaInfo(){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		String sql = "select * from tMaizuo_cinema";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		while(rs.next()){
			Map<String,String> map = new HashMap<String,String>();
			map.put("cinemaId", rs.getString("cinemaId"));
			map.put("address", rs.getString("address"));
			map.put("regionName", rs.getString("regionName"));
			map.put("tel", rs.getString("tel"));
			map.put("name", rs.getString("name"));
			map.put("cityPinyin", rs.getString("cityPinyin"));
			map.put("cityId", rs.getString("cityId"));
			resultList.add(map);
		}
		return resultList;
	}
	
	public List<Map<String,String>> getAllGewaraCinemaInfo(){
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		String sql = "select * from tGewara_cinema";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		while(rs.next()){
			Map<String,String> map = new HashMap<String,String>();
			map.put("title", rs.getString("title"));
			map.put("cityPinyin", rs.getString("cityPinyin"));
			map.put("regionName", rs.getString("regionName"));
			map.put("address", rs.getString("address"));
			map.put("cinemaCode", rs.getString("cinemaCode"));
			resultList.add(map);
		}
		return resultList;
	}
	
	public int updateCinemaForMaizuo(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "update tCinema_info set maizuoId=?,maizuoName=? where gewaraId = ?";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("maizuoId"));
				ps.setString(2, tempList.get(i).get("maizuoName"));
				ps.setString(3, tempList.get(i).get("gewaraId"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	public int updateCinemaForTaobao(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "update tCinema_info set taobaoId=?,taobaoName=? where gewaraId = ?";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("taobaoId"));
				ps.setString(2, tempList.get(i).get("taobaoName"));
				ps.setString(3, tempList.get(i).get("gewaraId"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
	public List<Map<String,String>> getAllCinemaInfo(){
		String sql = "select * from tMovie_info";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		while(rs.next()){
			Map<String,String> map = new HashMap<String,String>();
			map.put("gewaraMovieId", rs.getString("gewaraMovieId"));
			map.put("maizuoMovieId", rs.getString("maizuoMovieId"));
			map.put("movieName", rs.getString("movieName"));
			map.put("maizuoMovieName", rs.getString("maizuoMovieName"));
			map.put("taobaoMovieId", rs.getString("taobaoMovieId"));
			map.put("taobaoMovieName", rs.getString("taobaoMovieName"));
			resultList.add(map);
		}
		return resultList;
	}
	
	public Map<String,String> getAllCinemaInfoForRedis(){
		Map<String,String> map = new HashMap<String,String>();
		String sql = "select * from tCinema_info";
		Object[] params = {};
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql,params);
		while(rs.next()){
			JSONObject obj = new JSONObject();
			obj.put("gewaraId", rs.getString("gewaraId"));
			obj.put("gewaraName", rs.getString("gewaraName"));
			obj.put("maizuoId", rs.getString("maizuoId"));
			obj.put("maizuoName", rs.getString("maizuoName"));
			obj.put("maoyanId", rs.getString("maoyanId"));
			obj.put("wangpiaoId", rs.getString("wangpiaoId"));
			obj.put("cityPinyin", rs.getString("cityPinyin"));
			obj.put("taobaoId", rs.getString("taobaoId"));
			obj.put("taobaoName", rs.getString("taobaoName"));
			map.put(rs.getString("gewaraId"),JsonUtil.toJSON(obj));
		}
		return map;
	}
	
	
	public Map<String,Object> getCinemaMapByCinemaId(String cinemaId){
		Map<String,Object> map = new HashMap<String,Object>();
		String sql = "select * from tCinema_info where gewaraId=?";
		Object[] params = {cinemaId};
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql,params);
		while(rs.next()){
			map.put("gewaraId", rs.getString("gewaraId"));
			map.put("gewaraName", rs.getString("gewaraName"));
			map.put("maizuoId", rs.getString("maizuoId"));
			map.put("maizuoName", rs.getString("maizuoName"));
			map.put("maoyanId", rs.getString("maoyanId"));
			map.put("wangpiaoId", rs.getString("wangpiaoId"));
			map.put("cityPinyin", rs.getString("cityPinyin"));
		}
		return map;
	}
	
	public Map<String,String> getAllMovieForRedis(){
		Map<String,String> map = new HashMap<String,String>();
		String sql = "select * from tMovie_info";
		Object[] params = {};
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql,params);
		while(rs.next()){
			JSONObject obj = new JSONObject();
			obj.put("gewaraMovieId", rs.getString("gewaraMovieId"));
			obj.put("maizuoMovieId", rs.getString("maizuoMovieId"));
			obj.put("movieName", rs.getString("movieName"));
			obj.put("maizuoMovieName", rs.getString("maizuoMovieName"));
			obj.put("taobaoMovieId", rs.getString("taobaoMovieId"));
			obj.put("taobaoMovieName", rs.getString("taobaoMovieName"));
			map.put(rs.getString("gewaraMovieId"),JsonUtil.toJSON(obj));
		}
		return map;
	}
	
	public Map<String,Object> getMovieMapByMovieId(String movieId){
		Map<String,Object> map = new HashMap<String,Object>();
		String sql = "select * from tMovie_info where gewaraMovieId=?";
		Object[] params = {movieId};
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql,params);
		while(rs.next()){
			map.put("gewaraMovieId", rs.getString("gewaraMovieId"));
			map.put("maizuoMovieId", rs.getString("maizuoMovieId"));
			map.put("movieName", rs.getString("movieName"));
			map.put("maizuoMovieName", rs.getString("maizuoMovieName"));
		}
		return map;
	}
}
